package com.xiaoyi.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.WindowManager;

import com.avin.yi.R;
import com.xiaoyi.service.MusicService;
import com.xiaoyi.tool.AvBaseTool;
import com.xiaoyi.ui.fragment.MainFragment;
import com.xiaoyi.ui.fragment.base.BaseMainFragment;

import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;


public class MainActivity extends SupportActivity implements BaseMainFragment.OnBackToFirstListener {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        if (findFragment(MainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, MainFragment.newInstance());
        }
    }


    public static void startActivity(Activity context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
        context.finish();
    }

    @Override
    public void onBackPressedSupport() {
        super.onBackPressedSupport();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultHorizontalAnimator();
    }

    @Override
    public void onBackToFirstFragment(Fragment fragment) {

        // 在这里可以进行进一步的操作， 根据情况来判断是否退出还是回退到到第一个fragment
        exitActivity(this) ;
        ///  if (fragment instanceof PicFragment)


    }


    private static final long WAIT_TIME = 2000L;
    private long lastTime = 0;

    private void exitActivity(Activity activity) {
        if (System.currentTimeMillis() - lastTime < WAIT_TIME) {

            Intent intent = new Intent(MainActivity.this, MusicService.class);
            stopService(intent) ;


            activity.finish();
        } else {
            lastTime = System.currentTimeMillis();
            AvBaseTool.showMsg(activity, "双击退出");
        }
    }


}
